﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Ricciolo.PaperBoy.Feeds;

namespace Project_CFT2.ViewModel
{
    internal class PopupViewModel
    {
        private readonly FeedFolder _folder;
        //private static DataClasses1DataContext _dataDC = new DataClasses1DataContext();
        //private ObservableFurnitureItems _FurnitureItems;
        private static FurnitureDataClassesDataContext _dataDC = new FurnitureDataClassesDataContext();
        private Obs_Furn_Items _furnitureItems;

        public PopupViewModel()
        {
            
            _furnitureItems = new Obs_Furn_Items(_dataDC);
           // _FurnitureItems = new ObservableFurnitureItems(_dataDC);
        }
       
        //public FolderViewModel(Furniture i)
        //{
            
        //    _FurnitureItems = new ObservableFurnitureItems(_dataDC);
        //}

       // protected override string GetTitle() { return "Inventory"; }

        public bool IsRoot
        {
            get { return true; }
        }

        private ReadOnlyCollection<ViewModelBase> _items;

        public ReadOnlyCollection<ViewModelBase> Items
        {
            get
            {
                if (_items == null)
                {
                    var bw = new BackgroundWorker();
                    bw.DoWork += RefreshFoldersAndFeeds;
                    bw.RunWorkerAsync();
                }
                
                return _items;
            }
        }
       /* public ObservableFurnitureItems Items
        {
            get
            {


                return _furnitureItems;
            }
        }*/

        void RefreshFoldersAndFeeds(object sender, DoWorkEventArgs e)
        {
            //_folder.FoldersAndFeeds.Refresh();
            LoadFoldersAndFeeds();
        }

        private void LoadFoldersAndFeeds()
        {
            var foldersAndFeedsList = new List<ViewModelBase>();

            
            foreach (var item1 in _furnitureItems)
            {



                //foldersAndFeedsList.Add(new FurnitureViewModel(new myDataItem(item1.Name.ToString(), item1.Price.ToString(), item1.Type.ToString(), item1.Price.ToString(), item1.Image.ToString())));
                myFurnitureDataItem x1 = new myFurnitureDataItem(item1);
                foldersAndFeedsList.Add(new FurnitureViewModel(x1));
                //foldersAndFeedsList.Add(new FurnitureViewModel(new myFurnitureDataItem(item1)));
            }
            /*
            foreach (var feedBase in _folder.FoldersAndFeeds)
            {
                if (feedBase is FeedFolder)
                {
                    var subFolder = feedBase as FeedFolder;
                    if (subFolder.TotalItemCount > 0)
                        foldersAndFeedsList.Add(new FolderViewModel(subFolder));
                }
                else if (feedBase is Feed)
                {
                    var feed = feedBase as Feed;
                    if (feed.ItemCount > 0)
                        foldersAndFeedsList.Add(new FurnitureViewModel(feed));
                }
            }*/
            
            _items = new ReadOnlyCollection<ViewModelBase>(foldersAndFeedsList);
            //RaisePropertyChanged("Items");
        }
    }
}